package com.unittec.zk.provider.scope;

import com.unittec.zk.sdk.root.common.FieldConstants;

import lombok.Data;

/**
 * 线程安全的缓存信息，用于公共信息的传递
 *
 * @author wuxinwei
 */
public class ThreadLocalScope {

    @Data
    public static class AuthData {
        String requestId;
        String token;
        String ip;
        String tenantId;
        String appId;
        String user;
        String userName;
        String appCode;
    }

    private final static ThreadLocalScopeCache THREAD_LOCAL_CACHE = new ThreadLocalScopeCache();

    public static AuthData getAuthData() {
        AuthData authData = new AuthData();
        authData.setIp(getClientIp());
        authData.setAppId(getAppId());
        authData.setAppCode(getAppCode());
        authData.setRequestId(getRequestId());
        authData.setTenantId(getTenantId());
        authData.setToken(getAuthorization());
        authData.setUser(getLoginUser());
        authData.setUserName(getLoginUserName());
        return authData;
    }

    public static void setAuthData(String requestId, String token, String appId, String tenantId, String ip,
        String user, String appCode, String userName) {
        THREAD_LOCAL_CACHE.removeAll();
        if (null != requestId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.REQUEST_ID, requestId);
        }

        if (null != token) {
            THREAD_LOCAL_CACHE.put(FieldConstants.REQ_AUTHORIZATION, token);
        }

        if (null != appId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.APP_ID, appId);
        }

        if (null != tenantId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.TENANT_ID, tenantId);
        }

        if (null != ip) {
            THREAD_LOCAL_CACHE.put(FieldConstants.CLIENT_IP, ip);
        }

        if (null != user) {
            THREAD_LOCAL_CACHE.put(FieldConstants.LOGIN_USER, user);
        }
        if (null != userName) {
            THREAD_LOCAL_CACHE.put(FieldConstants.LOGIN_USER_NAME, userName);
        }
        if (null != appCode) {
            THREAD_LOCAL_CACHE.put(FieldConstants.APP_CODE, appCode);
        }
    }

    public static void setAuthData(String requestId, String appId, String ip) {
        if (null != requestId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.REQUEST_ID, requestId);
        }

        if (null != appId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.APP_ID, appId);
        }

        if (null != ip) {
            THREAD_LOCAL_CACHE.put(FieldConstants.CLIENT_IP, ip);
        }
    }

    public static void setAuthData(String tenantId, String appId) {
        if (null != tenantId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.TENANT_ID, tenantId);
        }

        if (null != appId) {
            THREAD_LOCAL_CACHE.put(FieldConstants.APP_ID, appId);
        }
    }

    public static void setAuthData(String user) {
        if (null != user) {
            THREAD_LOCAL_CACHE.put(FieldConstants.LOGIN_USER, user);
        }
    }

    private static String getTenantId() {
        String tenantId = (String)THREAD_LOCAL_CACHE.get(FieldConstants.TENANT_ID);
        return tenantId == null ? "" : tenantId;
    }

    private static String getAppCode() {
        String appCode = (String)THREAD_LOCAL_CACHE.get(FieldConstants.APP_CODE);
        return appCode == null ? "" : appCode;
    }

    public static String getAppId() {
        Object appId = THREAD_LOCAL_CACHE.get(FieldConstants.APP_ID);
        return appId == null ? "" : appId + "";
    }

    private static String getLoginUser() {
        Object user = THREAD_LOCAL_CACHE.get(FieldConstants.LOGIN_USER);
        return user == null ? "" : user + "";
    }

    private static String getLoginUserName() {
        Object user = THREAD_LOCAL_CACHE.get(FieldConstants.LOGIN_USER_NAME);
        return user == null ? "" : user + "";
    }

    private static String getAuthorization() {
        String token = (String)THREAD_LOCAL_CACHE.get(FieldConstants.REQ_AUTHORIZATION);
        return token == null ? "" : token;
    }

    private static String getClientIp() {
        String ip = (String)THREAD_LOCAL_CACHE.get(FieldConstants.CLIENT_IP);
        return ip == null ? "" : ip;
    }

    private static String getRequestId() {
        String requestId = (String)THREAD_LOCAL_CACHE.get(FieldConstants.REQUEST_ID);
        return requestId == null ? "" : requestId;
    }

}
